namespace Gateway.Common.Utils;

/// <summary>
/// level code 工具类
/// </summary>
public static class LevelCodeUtil
{
    /// <summary>
    /// 顶级的level code
    /// </summary>
    public const string LevelRootCode = "00000000";
    /// <summary>
    /// 单个level code 长度
    /// </summary>
    private const int LevelCodeSingleLength = 8;

    /// <summary>
    /// 获取父级level code
    /// </summary>
    /// <param name="levelCode"></param>
    /// <returns></returns>
    public static string GetParentLevelCode(string levelCode)
    {
        var parentLevelCode = levelCode.Substring(0, levelCode.Length - LevelCodeSingleLength);
        return parentLevelCode;
    }

    /// <summary>
    /// 获取下一个level code
    /// </summary>
    /// <param name="levelCode"></param>
    /// <returns></returns>
    public static string GetNextLevelCode(string levelCode)
    {
        string prefix=levelCode.Substring(0,levelCode.Length - LevelCodeSingleLength);
        var currentLevelCode = levelCode.Substring(levelCode.Length - LevelCodeSingleLength);
        var code = int.Parse(currentLevelCode);
        return prefix+((code + 1).ToString().PadLeft(LevelCodeSingleLength,'0'));
    }
    /// <summary>
    /// 获取上一个level code
    /// </summary>
    /// <param name="levelCode"></param>
    /// <returns></returns>
    public static string GetPreLevelCode(string levelCode)
    {
        var currentLevelCode = levelCode.Substring(levelCode.Length - LevelCodeSingleLength);
        var code = int.Parse(currentLevelCode);
        if (code == 0)
            return "";
        return (code - 1).ToString().PadLeft(LevelCodeSingleLength,'0');
    }
    /// <summary>
    /// 获取当前的层级
    /// </summary>
    /// <param name="levelCode"></param>
    /// <returns></returns>
    public static int GetLevel(string levelCode)
    {
        return levelCode.Length / LevelCodeSingleLength;
    }


    /// <summary>
    /// 是否同一层级
    /// </summary>
    /// <param name="levelCode1"></param>
    /// <param name="levelCode2"></param>
    /// <returns></returns>
    public static bool IsLevelEqual(string levelCode1, string levelCode2)
    {
        if (levelCode1.Length.Equals(levelCode2.Length))
        {
            return GetParentLevelCode(levelCode1).Equals(GetParentLevelCode(levelCode2));
        }

        return false;
    }

    /// <summary>
    /// 获取排序序号
    /// </summary>
    /// <param name="levelCode"></param>
    /// <returns></returns>
    public static int GetIndex(string levelCode)
    {
        if (levelCode.Length == LevelCodeSingleLength)
        {
            return int.Parse(levelCode);
        }
        var code = levelCode.Substring(levelCode.Length-LevelCodeSingleLength);
        return int.Parse(code);
    }

    /// <summary>
    /// 替换 level code 
    /// </summary>
    /// <param name="sourceLevelCode"></param>
    /// <param name="replaceCode"></param>
    /// <param name="targetLevelCode"></param>
    /// <returns></returns>
    public static string ReplaceLevelCode(string sourceLevelCode, string replaceCode, string targetLevelCode)
    {
        var val = sourceLevelCode.Substring(replaceCode.Length);
        return targetLevelCode + val;
    }


    public static string ConcatLevelCode(string levelCode1, string levelCode2)
    {
        return string.Concat(levelCode1, levelCode2);
    }
}